using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Infrastructure.Log;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog.Config;
using NLog.Web;


namespace FacadeApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((host, config) => {
                    var fullpath = typeof(Program).Assembly.Location;
                    var index = fullpath.LastIndexOf("/");
                    if (index<0)
                    {
                        index= fullpath.LastIndexOf("\\");
                    }
                    string dir = fullpath.Substring(0, index);

                    foreach (var item in Directory.GetFiles(dir, "config_*.json"))
                    {
                        config.AddJsonFile(item);
                    }
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.ConfigureKestrel(serverOptions =>
                    {
                        // Set properties and call methods on options
                        serverOptions.AddServerHeader = false;
                        serverOptions.ConfigureEndpointDefaults(listenOptions =>
                        {
                            listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
                            listenOptions.UseHttps("cacert.p12");
                        });

                        //serverOptions.Limits.MaxConcurrentConnections = 100;
                        //serverOptions.Limits.MaxConcurrentUpgradedConnections = 100;
                        //serverOptions.Limits.MaxRequestBodySize = 10 * 1024;
                        //serverOptions.Limits.MinRequestBodyDataRate =
                        //    new MinDataRate(bytesPerSecond: 100,
                        //        gracePeriod: TimeSpan.FromSeconds(10));
                        //serverOptions.Limits.MinResponseDataRate =
                        //    new MinDataRate(bytesPerSecond: 100,
                        //        gracePeriod: TimeSpan.FromSeconds(10));
                        //serverOptions.Limits.KeepAliveTimeout =
                        //    TimeSpan.FromMinutes(2);
                        //serverOptions.Limits.RequestHeadersTimeout =
                        //    TimeSpan.FromMinutes(1);
                    }).UseStartup<Startup>();
                })
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureLogging(logging =>
                {
                    var config = logging.Services.First(i => i.ServiceType == typeof(IHostEnvironment));
                    logging.ClearProviders();
                    logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
                    logging.AddGoLifeLog(((IHostEnvironment)config.ImplementationInstance));

                }).UseNLog();
    }
}
